/**
 * 主模块，负责菜单显示和处理用户操作
 */
public class CustomerView {

    private CustomerList customerList=new CustomerList(10);//创建一个长度为10的客户对象数组

    public CustomerView() {
        Customer cust=new Customer("刘明福",'男',22,"19983604254","2404438238@qq.com");
        customerList.addCustomer(cust);
    }

    /**
     * 客户信息管理软件的初始化界面
     */
    public void enterMainMenu(){
        boolean isFlag=true;
        while (isFlag){

            System.out.println("\n-----------------客户信息管理软件-----------------\n");
            System.out.println("                   1 添 加 客 户");
            System.out.println("                   2 修 改 客 户");
            System.out.println("                   3 删 除 客 户");
            System.out.println("                   4 客 户 列 表");
            System.out.println("                   5 退       出\n");
            System.out.print("                   请选择(1-5)：");

            //调用工具类CMUtility中方法
            char c = CMUtility.readMenuSelection();
            switch (c){
                //1 添 加 客 户 功 能
                case '1':
                    addNewCustomer();
                    break;
                //2 修 改 客 户 功 能
                case '2':
                    modifyCustomer();
                    break;
                //3 删 除 客 户 功 能
                case '3':
                    deleteCustomer();
                    break;
                //4 客 户 列 表 功 能
                case '4':
                    listAllCustomers();
                    break;
                //5 退 出 功 能
                case '5':
                    System.out.println("确认是否退出(Y/N)：");
                    char c1 = CMUtility.readConfirmSelection();
                    if (c1 == 'Y') {
                        isFlag=false;
                    }
                    break;

            }
        }
    }

    /**
     * 添加客户操作
     */
    private void addNewCustomer(){
        System.out.println("---------------------添加客户---------------------");
        System.out.print("姓名：");
        String name= CMUtility.readString(10);
        System.out.print("性别：");
        char sex = CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(15);
        System.out.print("邮箱：");
        String email = CMUtility.readString(15);

        //将信息封装到Customer对象中
        Customer cust=new Customer(name,sex,age,phone,email);
        boolean b = customerList.addCustomer(cust);

        //判断是否添加成功
        if (b == true) {
            System.out.println("---------------------添加成功---------------------");
        }else{
            System.out.println("---------------------添加失败---------------------");

        }
    }

    /**
     * 修改客户操作
     */
    private void modifyCustomer(){
        Customer cust=null;
        int index=0;
        System.out.println("---------------------修改客户---------------------");
        for (; ;) {
            System.out.print("请选择待修改客户编号(-1退出)：");
            //若输入-1，则退出修改界面
            index = CMUtility.readInt();
            if (index == -1) {
                return;
            }

            //若输入的不为客户编号
            cust = customerList.getCustomer(index);
            if (cust == null) {
                System.out.println("无法找到指定客户");
                //return;
            }else{//找到指定修改客户
                break;
            }
        }
            //若输入正确客户编号
        //修改客户信息
        System.out.print("姓名(" + cust.getName() + ")：");
        String name = CMUtility.readString(4, cust.getName());

        System.out.print("性别(" + cust.getGender() + ")：");
        char gender = CMUtility.readChar(cust.getGender());

        System.out.print("年龄(" + cust.getAge() + ")：");
        int age = CMUtility.readInt(cust.getAge());

        System.out.print("电话(" + cust.getPhone() + ")：");
        String phone = CMUtility.readString(15, cust.getPhone());

        System.out.print("邮箱(" + cust.getEmail() + ")：");
        String email = CMUtility.readString(15, cust.getEmail());

        //将信息封装到对象中
        cust = new Customer(name, gender, age, phone, email);
        boolean b = customerList.replaceCustomer(index - 1 , cust);
        if (b == true) {
            System.out.println("---------------------修改成功---------------------");
        }else {
            System.out.println("---------------------修改失败---------------------");

        }

    }

    /**
     * 删除客户操作
     */
    private void deleteCustomer(){
        System.out.println("---------------------删除客户---------------------");
        Customer cust=null;
        int index=0;
        for(;;){
            System.out.print("请选择待删除客户编号(-1退出)：");
            //若输入-1，则退出修改界面
            index = CMUtility.readInt();
            if (index == -1) {
                return;
            }

            //若输入的不为客户编号
            cust = customerList.getCustomer(index);
            if (cust == null) {
                System.out.println("无法找到指定客户");
                //return;
            }else{//找到指定删除客户
                break;
            }
        }
        //找到了指定客户
        System.out.println("确认是否删除(Y/N)：");
        char chr = CMUtility.readConfirmSelection();
        if (chr == 'Y' ) {
            boolean b = customerList.deleteCustomer(index-1);
            if (b == true) {
                System.out.println("---------------------删除成功---------------------");
            }else {
                System.out.println("---------------------删除失败---------------------");
            }
        }else{
            return;
        }




    }

    /**
     * 显示客户列表操作
     */
    private void listAllCustomers(){
        System.out.println("---------------------------客户列表---------------------------");
        int total = customerList.getTotal();
        if (total == 0) {
            System.out.println("没有客户记录");
        }else {
            System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱");
            //遍历打印
            Customer[] allCustomer = customerList.getAllCustomer();
            for (int i = 0; i < allCustomer.length; i++) {
                Customer cust = allCustomer[i];
                System.out.println((i+1)+"\t\t"+cust.getName()
                        +"\t\t"+cust.getGender()+"\t\t"+cust.getAge()
                        +"\t\t"+cust.getPhone()+"\t\t"+cust.getEmail());
            }
        }


        System.out.println("-------------------------客户列表完成-------------------------");
    }


    public static void main(String[] args){
        CustomerView customerView=new CustomerView();//创建一个控制主模块的对象
        customerView.enterMainMenu();

    }

}
